package org.example.petitionplatformsystem.controller;
import org.example.petitionplatformsystem.Response;
import org.example.petitionplatformsystem.dao.model.Departments;
import org.example.petitionplatformsystem.service.DepartmentsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/admin_panel/institution/{institutionId}")
public class DepartmentController {
    //部门表的服务
    private final DepartmentsService departmentsService;
    @Autowired
    public DepartmentController(DepartmentsService departmentsService){
        this.departmentsService = departmentsService;
    }

    @PostMapping("/department")
    public Response<Departments> addDepartment(@PathVariable Long institutionId,
                                               @RequestParam String departmentName,
                                               @RequestParam(required = false) String description) {
        Departments department = new Departments();
        department.setDepartmentName(departmentName);
        department.setDescription(description);
        department.setInstitutionId(institutionId); // Assuming Departments has a setInstitutionId method
        Departments createdDepartment = departmentsService.AddDepartments(department);
        return Response.newSuccessResponse(createdDepartment);
    }

    @DeleteMapping("/department/{departmentId}")
    public Response<Void> deleteDepartment(@PathVariable Long institutionId, @PathVariable Long departmentId) {
        Departments department = departmentsService.GetADepartment(departmentId, institutionId);
        if (department == null) {
            return Response.newFailResponse("Department not found");
        }
        departmentsService.DeleteDepartments(departmentId);
        return Response.newSuccessResponse(null);
    }

    @PutMapping("/department/{departmentId}")
    public Response<Departments> updateDepartment(@PathVariable Long institutionId,
                                                  @PathVariable Long departmentId,
                                                  @RequestParam String departmentName,
                                                  @RequestParam(required = false) String description) {
        Departments existingDepartment = departmentsService.GetADepartment(departmentId, institutionId);
        if (existingDepartment == null) {
            return Response.newFailResponse("Department not found");
        }
        existingDepartment.setDepartmentId(departmentId); // Assuming Departments has a setId method
        existingDepartment.setDepartmentName(departmentName);
        existingDepartment.setDescription(description);
        existingDepartment.setInstitutionId(institutionId); // Assuming Departments has a setInstitutionId method
        Departments updatedDepartment = departmentsService.UpdateDepartments(existingDepartment);
        return Response.newSuccessResponse(updatedDepartment);
    }

//    @GetMapping("/departments")
//    public Response<List<Departments>> getAllDepartments(@PathVariable Long institutionId) {
//        List<Departments> departmentsList = departmentsService.GetAllDepartments();
//        List<Departments> filteredList = departmentsList.stream()
//                .filter(dept -> dept.getInstitutionId().equals(institutionId))
//                .toList();
//        return Response.newSuccessResponse(filteredList);
//    }

    @GetMapping("/department/{departmentId}")
    public Response<Departments> getDepartment(@PathVariable Long institutionId, @PathVariable Long departmentId) {
        Departments department = departmentsService.GetADepartment(departmentId, institutionId);
        if (department != null) {
            return Response.newSuccessResponse(department);
        } else {
            return Response.newFailResponse("Department not found");
        }
    }
}



